/**
 * <p>多选的用户树控件</p>
 * xtype = cceafmultiselectuser

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.MultiSelectUser
 * @extends Ext.Panel
 * @since 1.0
 */
Ext.define('cceaf.control.MultiSelectUser',{
	extend: 'Ext.Panel', 
    alias: 'widget.cceafmultiselectuser',

	constructor: function(config){
		config = config || {};
		
	    var groupTree = new Ext.tree.Panel({
	    	multiSelect: true,
	        //el:'tree-div',
	        autoScroll:true,
	        animate:true,
	        enableDD:true,
	        containerScroll: true,
	        border: false,
	        //width:150,
	        flex: 1,
	        
	        root: {
	            expanded: true,
	            text: cceaf.util.AjaxHelper.getGroupName('1'),//返回组织ID=1的组织名称，组织ID定为组织的根节点
		        draggable:false,
		        id:'1'
	        },
	        
	        store: new Ext.data.TreeStore({
	            proxy: {
	                type: 'ajax',
	                url:  'pageDataProvider!getGroupTreeList.action',
	                extraParams: {
	                	groupid: '1'
	                }
	            },
	            model: 'cceaf.model.main.TreeNodeModel'
	        }),
	        
	        listeners: {
	        	'itemclick': function(view, record){
	        		var groupId = record.get('id');
	        		var groupName = record.get('text');
	        		
	        		userGrid.getStore().load({params:{groupId: groupId}});
	        	}
	        }
        
	    });
    
	    //多选列模式
		var selModel = new Ext.selection.CheckboxModel();
	
    	var userGrid = new cceaf.control.ViewGridPanel({
	    	closeButton: false,
	    	model: 'cceaf.model.main.SelectUserModel',
	    	selModel: selModel,
	    	flex: 1,
	    	queryAction: 'select/select!query.action?type=USER',
	    	columns: [
	    	    {xtype: 'rownumberer'},
		        {dataIndex: 'groupId', header: '机构编号', sortable: true, hidden: true}, 
		        {dataIndex: 'userId', header: '人员编号', sortable: true, flex: 1},
		        {dataIndex: 'userName', header: '人员名称', sortable: true, flex: 3},
		        {dataIndex: 'groupName', header: '所属机构', sortable: true, flex: 3}    	
	    	]
		});
	
	    //userTree.setRootNode(userRoot);

		config = Ext.applyIf(config || {}, {
			layout: 'fit',
	        autoScroll:true,
	        items: [
		        {
					layout: {
				        type: 'hbox',
				        align: 'stretch'
				    },
				    baseCls:'x-plain',
					items: [
						groupTree, 
						userGrid
		            ]
	            }
	        ],
	        
	        buttons: [
               	{xtype: 'button',text: ' 确  定 ',pressed: true,width: 70,
               		handler: function(){ 
               			var multiSelectUser = this.up('cceafmultiselectuser');
               			
               			var checkedRecords = userGrid.getSelectionModel().getSelection();
               			if (checkedRecords.length == 0) {
							cceaf.util.ExtHelper.showInfoDialog('请选择用户！');
							return;
						}
						
               			//提交选择的数据
               			multiSelectUser.submitData(checkedRecords);
               			    	
               			var multiSelectUserWin = multiSelectUser.findParentByType('window');
				    	if (multiSelectUserWin) {
				    		multiSelectUserWin.close();
				    	}
              		}
               },
               
               {xtype: 'button',text: ' 关  闭 ',pressed: true,width: 70,
               		handler: function(){
               			var multiSelectUser = this.up('cceafmultiselectuser');
               			var multiSelectUserWin = multiSelectUser.up('window');
				    	if(multiSelectUserWin){
				    		multiSelectUserWin.hide();
				    	}
               		}
               }
               
	        ]
		});

		cceaf.control.MultiSelectUser.superclass.constructor.apply(this, arguments);
		
		//展开根节点
		//userRoot.expand();
	},
	
	/**
	* 判断store中是否存在同样id的用户
	* 
	* @param {Ext.data.Store} store 组织stroe
	* @param {Number} fieldName 待填充的用户编号字段名称
	* @param {Number} id 用户编号
	* @return {Boolean} 存在返回true
	*/
	isExistSame: function(store, fieldName, id){
		var count = store.getCount();
		for	(i=0;i<count;i++) {
			record = store.getAt(i);
			if (record.get(fieldName) == id) {
				return true;
			}
		}
		
		return false;
	},
	
	/**
	* 选择后需要执行该方法把选中的数据填充，子类需要重载该方法
	* 
	* @param {Ext.data.Model[]} selectedItem 选中的用户
	*/
	submitData: function(selectedItem){
		cceaf.util.ExtHelper.showInfoDialog('请重载cceafmutilselectuser的submitData方法');
	}
	
});